package do1.mobo.widget;

import do1.common.json.JSONObject;
import do1.mobo.constant.Constant;
import do1.mobo.engine.MoboProtocolDataObject;
import do1.mobo.graphics.ImageFactory;
import android.content.Context;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

public class WidgetFactory 
{
	static public View createWidget(String type,JSONObject json,MoboProtocolDataObject moboProtocolDataObject,Context context,ImageFactory imageFactory,int parentWidth,String containerId)
	{
		View view=null;

		if(type==null 
				|| type.equalsIgnoreCase(Constant.VIEW_TYPE_LABEL)
				|| type.equalsIgnoreCase(Constant.VIEW_TYPE_HYPERLINK))//TextView
		{
			TextViewWidget tv=new TextViewWidget(context);
			tv.parse(json, imageFactory , parentWidth,containerId);
			view=tv;
		}
		else if(type.equalsIgnoreCase(Constant.VIEW_TYPE_IMAGE))//ImageView
		{
			ImageViewWidget imageView=new ImageViewWidget(context);
			imageView.parse(json, imageFactory,moboProtocolDataObject,parentWidth);
			boolean isEnclosure=imageView.isEnclosure();
			if(isEnclosure)
			{
				ScrollView hsv=(ScrollView) LayoutUtil.getScrollView(context, LayoutUtil.getLayoutParamsFillParent());
				HorizontalScrollView sv=(HorizontalScrollView)LayoutUtil.getHScrollView(context, LayoutUtil.getLayoutParamsFillParent());
				sv.addView(imageView);
				hsv.addView(sv);
				view=hsv;
			}
			else
			{
				view=imageView;
			}
		}
		else if(type.equalsIgnoreCase(Constant.VIEW_TYPE_EDITBOX) 
				|| 	type.equalsIgnoreCase(Constant.VIEW_TYPE_SECRETEDITBOX))//EditText
		{
			EditTextWidget tv=new EditTextWidget(context);
			tv.parse(json, imageFactory,parentWidth);
			view=tv;
		}
		else if ( type.equalsIgnoreCase(Constant.VIEW_TYPE_CHECKBOX) )
		{
			CheckBoxGroupWidget checkBoxGroupWidget=new CheckBoxGroupWidget(context);
			checkBoxGroupWidget.parse(json, imageFactory);
			view = checkBoxGroupWidget;
		}
		else if ( type.equalsIgnoreCase(Constant.VIEW_TYPE_RADIOBOX) )
		{
			RadioGroupWidget radioGroupWidget=new RadioGroupWidget(context);
			radioGroupWidget.parse(json, imageFactory);
			view = radioGroupWidget;
		}
		else if ( type.equalsIgnoreCase(Constant.VIEW_TYPE_DATETIME) )
		{
			DateTimeWidget dt=new DateTimeWidget(context);
			dt.parse(json, imageFactory,parentWidth);
			view=dt;
		}
		else if ( type.equalsIgnoreCase(Constant.VIEW_TYPE_DROPDOWN) )
		{
			DropdownCtrl ctrl=new DropdownCtrl(context);
			ctrl.parse(json, imageFactory,parentWidth);
			view=ctrl;
		}
		else if(type.equalsIgnoreCase(Constant.VIEW_TYPE_FILESELECT))
		{
			FileSelectWidget ctrl=new FileSelectWidget(context);
			ctrl.parse(json, imageFactory,parentWidth);
			view=ctrl;
		}
		else if(type.equalsIgnoreCase(Constant.VIEW_TYPE_HIDDEN))
		{
			HiddenView hidden=new HiddenView(context);
			hidden.parse(json);
			view=hidden;
		}
		else if(type.equalsIgnoreCase(Constant.VIEW_TYPE_WEBVIEW))
		{
			WebViewWidget web=new WebViewWidget(context);
			web.parse(json, imageFactory, parentWidth);
			view=web;
		}
		
		return view;
	}
}
